﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<script src="/Scripts/jquery-1.12.0.min.js"></script>
	<script src="/Scripts/jquery.signalR-2.4.1.min.js"></script>
	<script src="/signalr/hubs"></script>
	<script src="/dist/push.js/push.min.js"></script>
</head>
<body>
	<div>
		<input id="js-input-message" placeholder="please input message" />
		<button onclick="test()">测试推送消息</button>
	</div>
	<script>
		var notificationHub;
		$(function () {
			notificationHub = $.connection.notificationHub;

			notificationHub.client.notificationReceive = function (title, message, imageUrl, redirectUrl) {
				console.log(redirectUrl);
				Push.create(title, {
					body: message,
					icon: imageUrl,
					timeout: 4000,
					onClick: function () {
						window.focus();
						this.close();
						if (redirectUrl == null || redirectUrl == '') {
							window.location.href = redirectUrl;
						}
					}
				});
			}

			setTimeout(function () {
				$.connection.hub.start().done(function () {
					alert("连接MyHub成功");
				}).fail(function () { alert("连接MyHub失败"); });
			}, 5000);

		});

		function test() {
			$.ajax({
				url: "/NotificationDemo/Test",
				type: "post",
				data: { message: $("#js-input-message").val() },
				success: function (data) {
					console.log("成功了吗?" + data)
				}
			});
		}
	</script>
</body>
</html>